package com.ybfenpan.controller.member.info;

import com.ybfenpan.model.Member;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.QRCodeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * Created by lzx on 2017/5/26.
 */
@Controller
public class QRCodeController {

    @RequestMapping("/member/info/qrcode.shtml")
    public String qrcode() {
        return "member/info/qrcode";
    }

    @RequestMapping("/member/info/qrcode/render")
    public void renderQRCode(HttpServletResponse response, HttpServletRequest request) throws Exception {

        Member member = ContextUtil.getMember();

        //获取全路径
        StringBuffer requestURL = request.getRequestURL();
        //获取域名
        requestURL.delete(requestURL.length() - request.getRequestURI().length(), requestURL.length()).append("/qrcode/");

        try (OutputStream o = response.getOutputStream()) {
            QRCodeUtils.urlGenerate(requestURL.append(member.getLoginName()).toString(), o);
        }

    }

}
